package com.pets.springboot_petshop.controller;

import com.pets.springboot_petshop.entity.Shoppingcart;
import com.pets.springboot_petshop.entity.User;
import com.pets.springboot_petshop.entity.Useraddress;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.UserService;
import com.pets.springboot_petshop.service.UseraddressService;
import com.pets.springboot_petshop.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/15 上午11:54 周二
 * @Description: 地址控制层
 */
@RestController
@RequestMapping("/Useraddress")
public class UseraddressController {
    @Autowired
    private UseraddressService useraddressService;
    @Autowired
    private UserService userService;
    //id查询
    @GetMapping("select")
    private Result select(@RequestParam int id) {
        Useraddress useraddress = useraddressService.select(id);
        if (useraddress != null) {
            return Result.success(useraddress);
        }else {
            return Result.error("获取失败");
        }
    }
    //user_id查询
    @GetMapping("selectuser_id")
    private Result selectuser_id(@RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String,Object> map = JwtUtil.parseToken(token);
        int user_id = (int) map.get("id");
        // 检查用户是否存在
        User user = userService.select(user_id);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
        List<Useraddress> useraddress = useraddressService.selectuser_id(user_id);
        if (useraddress != null) {
            return Result.success(useraddress);
        }else {
            return Result.error("获取失败");
        }}
    }
    //查询所有
    @GetMapping("/findAll")
    private Result findAll() {
        List<Useraddress> useraddress = useraddressService.findAll();
        if(useraddress == null || useraddress.isEmpty()){
            return Result.error("获取失败");
        }else {
            return Result.success(useraddress);
        }
    }
    //添加
    @PostMapping("add")
    private Result add(@RequestBody Useraddress useraddress,@RequestHeader(name = "Authorization") String token) {
        // 从令牌中解析用户ID
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer user_id = (Integer) map.get("id");

        if (user_id == null) {
            return Result.error("用户不存在");
        }
        useraddress.setUser_id(user_id);
        if (useraddressService.add(useraddress)>0){
            return Result.success("新增成功");
        }else {
            return Result.error("获取失败");
        }
    }

    //删除
    @PostMapping("delete")
    private Result delete(@RequestParam Integer id){
        if (useraddressService.delete(id)>0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    //用户删除
    @PostMapping("deleteUser")
    private Result deleteUser(@RequestBody Useraddress useraddress,@RequestHeader(name = "Authorization") String token){
        // 从令牌中解析用户ID
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer user_id = (Integer) map.get("id");

        if (user_id == null) {
            return Result.error("用户不存在");
        }
        useraddress.setUser_id(user_id);
        if (useraddressService.deleteUser(useraddress)>0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    //修改数据
    @PostMapping("update")
    private Result update(@RequestBody Useraddress useraddress){
        if (useraddressService.update(useraddress)>0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

}
